#define _STR_MODULE_

#include "stm32f4xx.h"
#include "osObjects.h"
#include "strctr.h"
//#include "ff.h"
//#include "spi_sd.h"
#include "stdio.h"
#include "com_hal.h"

int stStorageMain(void);
//FRESULT open_append (FIL* fp,const char* path);

union { 
    uint8_t bytes[4096];
    uint16_t words[2048];
    uint32_t dwords[1024];
} Gd25DataBuff;
/*
FRESULT FsSt;
FATFS Sdfs;
FIL fil;

SD_Error SdSt;
SD_CardInfo SdInfo;

void stNVIC_Configuration(void);
*/
/*****************************************************************************//*!
*
* @brief   led task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void StrCtrTask(void const *argument) {
    uint16_t Cnt;
    
   // gdSpiInit();
    Cnt = 0;
    osDelay(1000);
    
    stStorageMain();
    
    //FsSt = f_mount(&Sdfs,"",0);
    //open_append(&fil,"app.c");

    for (;;) {
        
        osDelay(5000);

        osMutexWait ( mid_StrMutex, 0);
        //gd25qRead(Cnt, 1024, Gd25DataBuff);
        osMutexRelease (mid_StrMutex);
        
        Cnt++;
        if(Gd25DataBuff.bytes[0]){
        }
    }
}

/*****************************************************************************//*!
*
* @brief   led task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************//*
FRESULT open_append (
    FIL* fp,            // [OUT] File object to create 
    const char* path    // [IN]  File name to be opened 
)
{
    FRESULT fr;
    UINT FsResdLen;
    FRESULT FsSt;

    // Opens an existing file. If not exist, creates a new file. 
    fr = f_open(fp, path, FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
    if (fr == FR_OK) {
        // Seek to end of the file to append data 
        fr = f_read(&fil, Gd25DataBuff.bytes, 1000, &FsResdLen);
        if (fr != FR_OK){
            f_close(fp);
            return fr;
        }
        
        fr = f_lseek(fp, f_size(fp)-512);
        if (fr != FR_OK)
            f_close(fp);
    }
    
    FsSt = fr;
    
    if (FsSt == FR_OK) {
        FsSt = f_read(&fil, &Gd25DataBuff.bytes[1000], 1000, &FsResdLen);
        f_close(&fil);
    }
    
    FsSt = f_open(&fil, "app.c", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
    if(FR_OK == FsSt){
        for(;;){
            FsSt = f_read(&fil, &Gd25DataBuff.bytes[0], 128, &FsResdLen);
            if(FR_OK == FsSt){
                Gd25DataBuff.bytes[FsResdLen] = 0;
                printf((char const*)Gd25DataBuff.bytes);
                osDelay(10);
                while(!ComTxStCheck()){osDelay(5);}
                if(128 > FsResdLen){
                    break;
                }
            }
        }
        f_close(&fil);
    }
    
    return fr;
}*/